from nicegui import ui
from geo_comp import get_orientation_with_degree, calculate_bearing, calculate_distance


def calculate_and_display():
    try:
        lat1 = float(input_lat1.value)
        lon1 = float(input_lon1.value)
        lat2 = float(input_lat2.value)
        lon2 = float(input_lon2.value)
        result = get_orientation_with_degree(lat1, lon1, lat2, lon2)
        distance = calculate_distance(lat1, lon1, lat2, lon2)
        bearing = calculate_bearing(lat1, lon1, lat2, lon2)
        result += f" 距离: {distance:.2f}km  方位角: {bearing:.2f}°"

        output_label.set_text(f"方位角: {result}")
    except ValueError:
        output_label.set_text("请输入有效的浮点数")


# 创建输入组件
input_lat1 = ui.input("纬度1", placeholder="输入纬度")
input_lon1 = ui.input("经度1", placeholder="输入经度")
input_lat2 = ui.input("纬度2", placeholder="输入纬度")
input_lon2 = ui.input("经度2", placeholder="输入经度")

# 创建计算按钮
ui.button("计算方位角", on_click=calculate_and_display)

# 创建输出标签
output_label = ui.label()

ui.run()
